﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

namespace GameDesigner
{
    public class TimelineStateMachineView : StateMachineView
    {
        public Animator animator;
        public PlayableDirector director;

        public override void Init(params object[] args)
        {
            controller.Init(this, (layer) => new TimelineStateMachine(animator, director, layer), args);
        }

#if UNITY_EDITOR
        public override void GetClipNames(Transform root, List<string> clipNames)
        {
            if (director == null)
                director = root.GetComponentInChildren<PlayableDirector>();
            if (animator == null)
                animator = root.GetComponentInChildren<Animator>();
            AnimatorStateMachineView.GetClipNames(animator, clipNames);
            if (animClipReference is IAnimationClipReference animationClipReference)
            {
                clipNames.Clear();
                clipNames.AddRange(animationClipReference.ClipNames);
            }
        }

        public override float GetAnimClipLength(StateAction action)
        {
            if (action.clipAsset != null)
                return (float)(action.clipAsset as PlayableAsset).duration;
            return AnimatorStateMachineView.GetAnimClipLength(animator, action);
        }
#endif
    }
}